#include "ParticleManager.h"
#include <tchar.h>
#include <string.h>

ParticleManager* ParticleManager::Instance = nullptr;

ParticleManager::ParticleManager(void)
{
	
}

ParticleManager::~ParticleManager(void)
{
	if(Instance == NULL)
		return;
}

void ParticleManager::ShutDown(void)
{
	unsigned int size = EmitterKeys.size();

	for(unsigned int i = 0; i < size; i++)
	{
		EmitterMap[EmitterKeys[i]]->ClearParticles();
		delete EmitterMap[EmitterKeys[i]];
		EmitterMap[EmitterKeys[i]] = nullptr;
	}
}

void ParticleManager::Render(string FileName)
{
	EmitterMap[FileName]->Render();
}

void ParticleManager::Update(float ElapsedTime, string KeyName)
{
	/*std::map<string, Emitter*>::iterator i = EmitterMap.begin();
	while(i != EmitterMap.end())
	{
		(*i).second->Update(ElapsedTime);
		++i;
	}*/

	EmitterMap[KeyName]->Update(ElapsedTime);
}

void ParticleManager::Update(float ElapsedTime)
{
	std::map<string, Emitter*>::iterator i = EmitterMap.begin();
	while(i != EmitterMap.end())
	{
		(*i).second->Update(ElapsedTime);
		++i;
	}
}

void ParticleManager::Reset(string FileName)
{
	EmitterMap[FileName]->ClearParticles();
	EmitterMap[FileName]->SetParticles((unsigned int)(EmitterMap[FileName]->GetMaxParticles()));
}

void ParticleManager::ResetParticles(string FileName)
{
	EmitterMap[FileName]->ResetAll();
}

ParticleManager* ParticleManager::GetInstance(void)
{
	if(Instance != nullptr)
		return Instance;
	else
		return Instance = new ParticleManager();
}

void ParticleManager::DeleteInstance(void)
{
	if(Instance != nullptr)
	{
		delete Instance;
		Instance = nullptr;
	}
}

bool ParticleManager::Load(const char* FileName)
{
	TiXmlDocument doc;

	if(doc.LoadFile(FileName) == false)
		return false;

	TiXmlElement* pRoot = doc.RootElement();

	if(!pRoot)
		return false;


	EmitterMap[FileName] = new Emitter();
	EmitterKeys.push_back(FileName);

	TiXmlElement* life = pRoot->FirstChildElement("Life");

	if(life)
	{
		double x = -1;

		if(life->Attribute("MinLife", &x) != NULL)
			EmitterMap[FileName]->SetMinLife((float)x);
		
		

		if(life->Attribute("MaxLife", &x) != NULL)
			EmitterMap[FileName]->SetMaxLife((float)x);
	}

	TiXmlElement* Color = pRoot->FirstChildElement("Color");
	
	if(Color)
	{
		double x = -1;

		if(Color->Attribute("StartColor.r", &x) != NULL)
			EmitterMap[FileName]->SetStartColorR((float)x);

		if(Color->Attribute("StartColor.g", &x) != NULL)
			EmitterMap[FileName]->SetStartColorG((float)x);

		if(Color->Attribute("StartColor.b", &x) != NULL)
			EmitterMap[FileName]->SetStartColorB((float)x);

		if(Color->Attribute("StartColor.a", &x) != NULL)
			EmitterMap[FileName]->SetStartColorA((float)x);

		if(Color->Attribute("EndColor.r", &x) != NULL)
			EmitterMap[FileName]->SetEndColorR((float)x);

		if(Color->Attribute("EndColor.g", &x) != NULL)
			EmitterMap[FileName]->SetEndColorG((float)x);

		if(Color->Attribute("EndColor.b", &x) != NULL)
			EmitterMap[FileName]->SetEndColorB((float)x);

		if(Color->Attribute("EndColor.a", &x) != NULL)
			EmitterMap[FileName]->SetEndColorA((float)x);
	}

	TiXmlElement* Velocity = pRoot->FirstChildElement("Velocity");

	if(Velocity)
	{
		double x = -1;

		if(Velocity->Attribute("VelocityX", &x) != NULL)
			EmitterMap[FileName]->SetVelX((float)x);

		if(Velocity->Attribute("VelocityY", &x) != NULL)
			EmitterMap[FileName]->SetVelY((float)x);

		if(Velocity->Attribute("EndVelocityX", &x) != NULL)
			EmitterMap[FileName]->SetEndVelX((float)x);

		if(Velocity->Attribute("EndVelocityY", &x) != NULL)
			EmitterMap[FileName]->SetEndVelY((float)x);
	}

	TiXmlElement* Rect = pRoot->FirstChildElement("Rect");

	if(Rect)
	{
		double x = -1;

		if(Rect->Attribute("Right", &x) != NULL)
			EmitterMap[FileName]->SetRectRight((LONG)x);

		if(Rect->Attribute("Bottom", &x) != NULL)
			EmitterMap[FileName]->SetRectBottom((LONG)x);

		if(Rect->Attribute("Top", &x) != NULL)
			EmitterMap[FileName]->SetRectTop((LONG)x);

		if(Rect->Attribute("Left", &x) != NULL)
			EmitterMap[FileName]->SetRectLeft((LONG)x);
	}

	TiXmlElement* Scale = pRoot->FirstChildElement("Scale");
	
	if(Scale)
	{
		double x = -1;

		if(Scale->Attribute("ScaleX", &x) != NULL)
			EmitterMap[FileName]->SetScaleX((float)x);

		if(Scale->Attribute("ScaleY", &x) != NULL)
			EmitterMap[FileName]->SetScaleY((float)x);

		if(Scale->Attribute("EndScaleX", &x) != NULL)
			EmitterMap[FileName]->SetEndScaleX((float)x);

		if(Scale->Attribute("EndScaleY", &x) != NULL)
			EmitterMap[FileName]->SetEndScaleY((float)x);
	}

	TiXmlElement* Loop = pRoot->FirstChildElement("Loop");

	if(Loop)
	{
		double x = -1;
		bool loops = false;

		if(Loop->Attribute("Looping", &x) != NULL)
		{

			if(x == 0)
				loops = false;
			else
				loops = true;

			EmitterMap[FileName]->SetLooping(loops);
		}
	}

	TiXmlElement* Blend = pRoot->FirstChildElement("Blend");

	if(Blend)
	{
		double x = -1;

		if(Blend->Attribute("SourceBlend", &x) != NULL)
			EmitterMap[FileName]->SetSourceBlend((int)x);

		if(Blend->Attribute("DestBlend", &x) != NULL)
			EmitterMap[FileName]->SetDestBlend((int)x);
	}

	TiXmlElement* Image = pRoot->FirstChildElement("Image");

	if(Image)
	{
		const char* x;
		string str;

		if(Image->Attribute("ImageID") != NULL)
		{
			x = Image->Attribute("ImageID");
			str = "resource";
			str = str + "//" + x;
			size_t image;
			TCHAR buffer[120] = {};
			mbstowcs_s(&image, buffer, 120, str.c_str(), 120);
			EmitterMap[FileName]->SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture(buffer));
		}
	}

	TiXmlElement* maxparticles = pRoot->FirstChildElement("MaxParticles");

	if(maxparticles)
	{
		double x = -1;

		if(maxparticles->Attribute("MaxParticles", &x) != NULL)
			EmitterMap[FileName]->SetMaxParticles((float)x);

		EmitterMap[FileName]->SetParticles((unsigned int)x);
	}

	return true;
}